package utils

import java.text.SimpleDateFormat
import java.util.*

private val yyyyMMdd = SimpleDateFormat("yyyy-MM-dd'T'HH:mm+08:00", Locale.getDefault())
private val calendar = Calendar.getInstance()

fun String.getDateWeekName(): String {
    val dateArray = split("-")
    val todayWeek = calendar.get(Calendar.DAY_OF_WEEK)
    calendar.clear()
    calendar.set(dateArray[0].toInt(), dateArray[1].toInt() - 1, dateArray[2].toInt())
    val weeks = arrayOf("周日", "周一", "周二", "周三", "周四", "周五", "周六")
    var weekIndex: Int = calendar.get(Calendar.DAY_OF_WEEK) - 1
    if (weekIndex < 0) {
        weekIndex = 0
    }
    return if (todayWeek == calendar.get(Calendar.DAY_OF_WEEK)) "今天" else weeks[weekIndex]
}

fun String?.getTimeNameForObs(): String {
    val calendar = Calendar.getInstance()
    calendar.time = yyyyMMdd.parse(this) ?: Date()
    return "${calendar.get(Calendar.HOUR_OF_DAY)}:${calendar.get(Calendar.MINUTE)}"
}

fun String.getTimeName(): String {
    val calendar = Calendar.getInstance()
    val todayHour = calendar.get(Calendar.HOUR_OF_DAY)
    calendar.time = yyyyMMdd.parse(this) ?: Date()
    val hour = calendar.get(Calendar.HOUR_OF_DAY)
    return if (todayHour + 1 == hour) "现在" else "${hour}时"
}